package com.me.study.concurrent.线程通信;

/**
 * 测试 Object对象的 wait() 和 notify()
 *
 * @author ME
 * @date   2019/7/4
 */
public class TestWaitAndNotify {

    public static void main(String[] args) {
        try {
            Object lock = new Object();

            ThreadA a = new ThreadA("Thread-A", lock);
            a.start();

            Thread.sleep(10000);

            a.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void testMethod(Object lock) {
        try {
            synchronized (lock) {
                System.out.println("begin wait()");
                lock.wait();
                System.out.println("end wait()");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("出现异常");
        }
    }
}

class ThreadA extends Thread {
    private String name;
    private Object lock;

    public ThreadA(String name, Object lock) {
        super(name);
        this.name = name;
        this.lock = lock;
    }

    @Override
    public void run() {
        TestWaitAndNotify test = new TestWaitAndNotify();
        test.testMethod(lock);
    }
}
